﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;

public partial class Return : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.GetCurrentUser() == null)
            Response.Redirect("~/Login/LoginForm.aspx?returnUrl=" + Request.Url.PathAndQuery);
        else
            if (Session.GetCurrentUser() != "Admin")
                Response.Redirect("~/Login/Permission.aspx");
        else
            if (!Page.IsPostBack)
            {
                LINQDataContext lin = new LINQDataContext();
                var rg = from a in lin.GetTable<Issues>()
                         join b in lin.GetTable<Book>()
                         on a.BookID equals b.BookID
                         join c in lin.GetTable<Logins>()
                         on a.UserName equals c.UserName
                         select new
                         {
                             a.IssueID,
                             a.UserName,
                             a.BookID,
                             b.BookName,
                             IssueDate = a.IssueDate.ToShortDateString(),
                             ReturnDate = a.ReturnDate.ToShortDateString(),
                             a.RenewTime
                         };
                GridView1.DataSource = rg;
                GridView1.DataBind();

                ////////////////////////
            }

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        LINQDataContext lin = new LINQDataContext();
        string dropText = DropDownList1.SelectedValue.ToString();

        switch (dropText)
        {
            case "[Search By]":
                break;
            case "Username":
                var rg = from a in lin.GetTable<Issues>()
                         join b in lin.GetTable<Book>()
                         on a.BookID equals b.BookID
                         where a.UserName == txtSearch.Text
                         select new
                         {
                             a.IssueID,
                             a.UserName,
                             a.BookID,
                             b.BookName,
                             IssueDate = a.IssueDate.ToShortDateString(),
                             ReturnDate = a.ReturnDate.ToShortDateString(),
                             a.RenewTime
                         };
                GridView1.DataSource = rg;
                GridView1.DataBind();
                break;
            case "BookID":
                var rg1 = from a in lin.GetTable<Issues>()
                          join b in lin.GetTable<Book>()
                          on a.BookID equals b.BookID
                          where a.BookID.ToString() == txtSearch.Text
                          select new
                          {
                              a.IssueID,
                              a.UserName,
                              a.BookID,
                              b.BookName,
                              IssueDate = a.IssueDate.ToShortDateString(),
                              ReturnDate = a.ReturnDate.ToShortDateString(),
                              a.RenewTime
                          };
                GridView1.DataSource = rg1;
                GridView1.DataBind();
                break;
            case "Book Name":
                var rg3 = from a in lin.GetTable<Issues>()
                          join b in lin.GetTable<Book>()
                          on a.BookID equals b.BookID
                          where b.BookName.Contains(txtSearch.Text)
                          select new
                             {
                                 a.IssueID,
                                 a.UserName,
                                 a.BookID,
                                 b.BookName,
                                 IssueDate = a.IssueDate.ToShortDateString(),
                                 ReturnDate = a.ReturnDate.ToShortDateString(),
                                 a.RenewTime
                             };
                GridView1.DataSource = rg3;
                GridView1.DataBind();
                break;
            default:
                break;
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "dele")
        {

            LINQDataContext lin = new LINQDataContext();

            var iss = from a in lin.GetTable<Issues>()
                      where a.IssueID.ToString() == e.CommandArgument.ToString()
                      select a;
            foreach (Issues d in iss)
            {
                lin.Issues.DeleteOnSubmit(d);
                lin.SubmitChanges();
                GridView1.DataBind();
                var rg = from a in lin.GetTable<Issues>()
                         join b in lin.GetTable<Book>()
                         on a.BookID equals b.BookID
                         select new
                         {
                             a.IssueID,
                             a.UserName,
                             a.BookID,
                             b.BookName,
                             IssueDate = a.IssueDate.ToShortDateString(),
                             ReturnDate = a.ReturnDate.ToShortDateString(),
                             a.RenewTime
                         };
                GridView1.DataSource = rg;
                GridView1.DataBind();
            }
        }
        if (e.CommandName == "renew")
        {

            LINQDataContext lin = new LINQDataContext();

            var iss = from a in lin.GetTable<Issues>()
                      where a.IssueID.ToString() == e.CommandArgument.ToString()
                      select a;
            foreach (Issues d in iss)
            {
                if (d.RenewTime == 0)
                    note.Text = "Cannot renew";
                else
                {
                    d.RenewTime--;
                    DateTime da = d.ReturnDate;
                    d.ReturnDate = da.AddDays(7);
                    lin.SubmitChanges();
                    GridView1.DataBind();
                    var rg = from a in lin.GetTable<Issues>()
                             join b in lin.GetTable<Book>()
                             on a.BookID equals b.BookID
                             select new
                             {
                                 a.IssueID,
                                 a.UserName,
                                 a.BookID,
                                 b.BookName,
                                 IssueDate = a.IssueDate.ToShortDateString(),
                                 ReturnDate = a.ReturnDate.ToShortDateString(),
                                 a.RenewTime
                             };
                    GridView1.DataSource = rg;
                    GridView1.DataBind();
                }
            }


        }
    }
    protected void showOverdue_Click(object sender, EventArgs e)
    {
        LINQDataContext lin = new LINQDataContext();
        DateTime now = DateTime.Now;
        var rg = from a in lin.GetTable<Issues>()
                 join b in lin.GetTable<Book>()
                             on a.BookID equals b.BookID
                 where now > a.ReturnDate
                 select new
                 {
                     a.IssueID,
                     a.UserName,
                     a.BookID,
                     b.BookName,
                     IssueDate = a.IssueDate.ToShortDateString(),
                     ReturnDate = a.ReturnDate.ToShortDateString(),
                     a.RenewTime
                 };
        GridView1.DataSource = rg;
        GridView1.DataBind();
    }
    protected void btnShowOverdue_Click(object sender, EventArgs e)
    {
        LINQDataContext lin = new LINQDataContext();
        DateTime now = DateTime.Now;
        var rg = from a in lin.GetTable<Issues>()
                 join b in lin.GetTable<Book>()
                             on a.BookID equals b.BookID
                 where now > a.ReturnDate
                 select new
                 {
                     a.IssueID,
                     a.UserName,
                     a.BookID,
                     b.BookName,
                     IssueDate = a.IssueDate.ToShortDateString(),
                     ReturnDate = a.ReturnDate.ToShortDateString(),
                     a.RenewTime
                 };
        GridView1.DataSource = rg;
        GridView1.DataBind();
    }
}